var user = "";
function tes(data) {
    user = data;
    $("#id").val(user.id);
    $("#name").val(user.userName);
    $("#yhm").val(user.loginName);
    $("#password").val(user.password);
    $("#hidden").val(user.password);
    $("#phone").val(user.phone);
    $("#img").attr("src","http://192.168.2.80:5050/images/wms/Avatar/"+user.headFileName);

}
var vm = new Vue({
    el: "#sel",
    data: {
        list: []
    },mounted(){
        $.get("htz/role/selChoice",function (data) {

            layui.use('form',function () {
                $.each(data.obj, function (index, item) {
                    $('#sel').append(new Option(item.roleName, item.id));
                })
                layui.form.render("select");
                var select = 'dd[lay-value=' + user.roleId + ']';
                $('#sel').siblings("div.layui-form-select").find('dl').find(select).click();
            })
            layui.use(['form', 'layedit', 'laydate','layer','element'], function(){
                var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
                var form = layui.form
                    ,layer = layui.layer
                    ,layedit = layui.layedit
                    ,laydate = layui.laydate;
                //日期
                laydate.render({
                    elem: '#date'
                });
                laydate.render({
                    elem: '#date1'
                });

                //创建一个编辑器
                var editIndex = layedit.build('LAY_demo_editor');

                //自定义验证规则
                form.verify({
                    title: function(value){
                        if(value.length < 5){
                            return '标题至少得5个字符啊';
                        }
                    }
                    ,pass: [
                        /^[\S]{6,12}$/
                        ,'密码必须6到12位，且不能出现空格'
                    ]
                    ,content: function(value){
                        layedit.sync(editIndex);
                    }
                });

                var select = 'dd[lay-value=' + user.roleId + ']';
                $('#sel').siblings("div.layui-form-select").find('dl').find(select).click();
                //监听指定开关
                form.on('switch(switchTest)', function(data){
                    layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
                        offset: '6px'
                    });
                    layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
                });
                var userNameb = true;
                var loginNameb = true;
                //监听提交
                form.on('submit(demo1)', function(data){
                    //所有的验证信息成功后才能进入这个方法
                    //data.field固定写法获取表单的JSON数据
                    // layer.alert(JSON.stringify(data.field), {
                    //     title: '最终的提交信息'
                    // })
                    // // layer.msg("判断成功")
                    // return false;
                    if (loginNameb){
                        if (userNameb){
                            var password = $("#password").val();
                            if ($("#password").val()===$("#hidden").val()){
                                password = '';
                            }
                            var json = {
                                id: data.field.id,
                                userName: data.field.userName,
                                password: password,
                                loginName: data.field.loginName,
                                phone: data.field.phone,
                                roleId: data.field.roleId
                            }
                            var da =new FormData();
                            da.append("json",JSON.stringify(json));
                            da.append("file",$("#file")[0].files[0])
                            $.ajax({
                                url: 'htz/user/userUpd?token=1',
                                type: 'post',
                                processData: false,
                                contentType: false,
                                data: da,
                                success: function (data) {
                                    alert(JSON.stringify(data))
                                    if (data.obj>0){
                                        layer.msg("修改成功")
                                        parent.relod();
                                    } else {
                                        layer.msg("修改失败")
                                    }
                                }
                            })
                        }else {
                            layer.msg("姓名重复")
                        }
                    } else {
                        layer.msg("用户名重复")
                    }
                });
                $("#name").blur(function () {
                    var userName = $("#name").val();
                    layer = layui.layer;
                    if (userName!==''){
                        $.post("htz/user/byUserName", {userName: userName, token: 1}, function (data) {
                            if (data.obj !=null) {
                                if (data.obj.id!=$("#id").val()){
                                    layer.msg("姓名重复,请重试")
                                    userNameb = false;
                                }
                            }else {
                                userNameb = true;
                            }
                        })
                    }
                })
                $("#img").click(function () {
                    $("#file").click();
                })
                $("#yhm").blur(function () {
                    var yhm = $("#yhm").val();
                    layer = layui.layer;
                    if (yhm!==''){
                        $.post("htz/user/byLoginName", {loginName: yhm, token: 1}, function (data) {
                            if (data.obj !=null) {
                                if (data.obj.id!=$("#id").val()){
                                    layer.msg("用户名重复,请重试")
                                    loginNameb = false;
                                }
                            }else {
                                loginNameb =true;
                            }
                        })
                    }
                })
                //表单赋值
                layui.$('#LAY-component-form-setval').on('click', function(){
                    form.val('example', {
                        "username": "贤心" // "name": "value"
                        ,"password": "123456"
                        ,"interest": 1
                        ,"like[write]": true //复选框选中状态
                        ,"close": true //开关状态
                        ,"sex": "女"
                        ,"desc": "我爱 layui"
                    });
                });

                //表单取值
                layui.$('#LAY-component-form-getval').on('click', function(){
                    var data = form.val('example');
                    alert(JSON.stringify(data));
                });

            });
            // var da = data;
            // vm.list = da.obj;

        })
    }
})
$(function () {
    $("#file").change(function(){
        $("#img").attr("src",URL.createObjectURL(this.files[0]));
    })
})